<?php

//获取创建的订单的信息,订单可以是未支付的，支付过的，

header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require './mysql_init.php';

//检查数据是否正常
function limit_up_data($session_id){
    $flag=1;
    if(gettype($session_id) != 'string'){
        $flag=0;
    }
    if(strlen($session_id) < 2 || strlen($session_id) > 300){
        $flag=0;
    }
    return $flag;
}
//获取邮箱
function get_email($pdo,$session_id){
    try {
        // 准备查询语句
        $query = "SELECT email FROM session WHERE session_id = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $session_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['email'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取uid
function get_uid($pdo,$email){
    try {
        // 准备查询语句
        $query = "SELECT uid FROM users WHERE email = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $email);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['uid'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取订单中的信息
function get_orders_news($pdo,$uid,$order_id){
    try {
        // 准备查询语句
        $query = "SELECT COUNT(*) FROM orders WHERE order_id =? AND user_uid =? ";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $order_id);
        $statement->bindParam(2, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['COUNT(*)'];      //把查询结构赋值给$count
        if($result==1){
            // 准备查询语句
            // $query2 = "SELECT g.name,o.order_price,o.count,sa.province ,sa.detailed_address ,sa.phone_number ,o.create_time  FROM goods g,orders o,shipping_address sa WHERE o.order_id =? AND g.goods_id =o.goods_id AND o.user_uid =sa.user_uid";
            $query2="SELECT g.name,o.order_price,o.count,sa.province ,sa.detailed_address ,sa.phone_number ,o.create_time,o.user_is_payment ,o.user_is_cancel ,o.shller_is_cancle ,o.is_send_out_goods 
            FROM goods g,orders o,shipping_address sa 
            WHERE o.order_id =? AND g.goods_id =o.goods_id AND o.address_id =sa.address_id";
            $statement2 = $pdo->prepare($query2);
            $statement2->bindParam(1, $order_id);
            // 执行查询
            $statement2->execute();
            // 获取结果
            $result2 = $statement2->fetchAll()[0];      //把查询结构赋值给$count
            return $result2;
        }else{
            return 0;
        }
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $json=file_get_contents('php://input');  //获取post请求中的原始数据
    $post_json=json_decode($json,true);       //把原始数据转化成json

    $session_id=$post_json['session_id'];
    $order_id=(int)$post_json['order_id'];
    
    $response = array(
        'status' => 0,
        'message' =>'初始化'
    );
    if(limit_up_data($session_id)==1){      //验证上传的session_id数据类型是否合法
        if(gettype(get_email($pdo,$session_id))=='string'){
            $email=get_email($pdo,$session_id);
            $uid=get_uid($pdo,$email);
            $order_news=get_orders_news($pdo,$uid,$order_id);
            if($order_news!=0){
                $response = array(
                    'status' => 1,
                    'message' =>'成功',
                    'data' =>$order_news
                );    
            }else{
                $response = array(
                    'status' => 0,
                    'message' =>'没有此订单'
                );
            }

        }else{
            $response = array(
                'status' => 0,
                'message' => '用户不存在或未登录'
            );
        }
    }else{
        $response = array(
            'status' => 0,
            'message' => '数据类型错误'
        );
    }

    echo json_encode($response);
} else{             //如果不是post请求
    $response = array(
        'status' => 0,
        'message' => '不是请求'
    );
    echo json_encode($response);
}
?>